﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

namespace ArticleManagementSystem.Api.Migrations
{
    public partial class 重新生成表 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_admin_Recommends_RecommendId",
                table: "admin");

            migrationBuilder.DropForeignKey(
                name: "FK_article_Recommends_RecommendId",
                table: "article");

            migrationBuilder.DropForeignKey(
                name: "FK_imgs_article_ArticleId",
                table: "imgs");

            migrationBuilder.DropForeignKey(
                name: "FK_imgs_Avatar_AvatarId",
                table: "imgs");

            migrationBuilder.DropForeignKey(
                name: "FK_imgs_carousel_CarouselId",
                table: "imgs");

            migrationBuilder.DropForeignKey(
                name: "FK_imgs_users_UsersId",
                table: "imgs");

            migrationBuilder.DropForeignKey(
                name: "FK_users_Like_LikeId",
                table: "users");

            migrationBuilder.DropTable(
                name: "ArticleUsers");

            migrationBuilder.DropTable(
                name: "AvatarUsers");

            migrationBuilder.DropTable(
                name: "CommentUsers");

            migrationBuilder.DropTable(
                name: "Avatar");

            migrationBuilder.DropIndex(
                name: "IX_users_LikeId",
                table: "users");

            migrationBuilder.DropIndex(
                name: "IX_imgs_ArticleId",
                table: "imgs");

            migrationBuilder.DropIndex(
                name: "IX_imgs_AvatarId",
                table: "imgs");

            migrationBuilder.DropIndex(
                name: "IX_imgs_CarouselId",
                table: "imgs");

            migrationBuilder.DropIndex(
                name: "IX_imgs_UsersId",
                table: "imgs");

            migrationBuilder.DropIndex(
                name: "IX_article_RecommendId",
                table: "article");

            migrationBuilder.DropIndex(
                name: "IX_admin_RecommendId",
                table: "admin");

            migrationBuilder.DropColumn(
                name: "LikeId",
                table: "users");

            migrationBuilder.DropColumn(
                name: "ArticleId",
                table: "imgs");

            migrationBuilder.DropColumn(
                name: "AvatarId",
                table: "imgs");

            migrationBuilder.DropColumn(
                name: "CarouselId",
                table: "imgs");

            migrationBuilder.DropColumn(
                name: "UsersId",
                table: "imgs");

            migrationBuilder.DropColumn(
                name: "CommentUserId",
                table: "article");

            migrationBuilder.DropColumn(
                name: "GoodUserId",
                table: "article");

            migrationBuilder.DropColumn(
                name: "RecommendId",
                table: "article");

            migrationBuilder.DropColumn(
                name: "RecommendId",
                table: "admin");

            migrationBuilder.AddColumn<int>(
                name: "ToCommentId",
                table: "Comments",
                type: "integer",
                nullable: false,
                defaultValue: 0);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "ToCommentId",
                table: "Comments");

            migrationBuilder.AddColumn<int>(
                name: "LikeId",
                table: "users",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "ArticleId",
                table: "imgs",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "AvatarId",
                table: "imgs",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "CarouselId",
                table: "imgs",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "UsersId",
                table: "imgs",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "CommentUserId",
                table: "article",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "GoodUserId",
                table: "article",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "RecommendId",
                table: "article",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "RecommendId",
                table: "admin",
                type: "integer",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "ArticleUsers",
                columns: table => new
                {
                    ArticlesId = table.Column<int>(type: "integer", nullable: false),
                    UsersId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ArticleUsers", x => new { x.ArticlesId, x.UsersId });
                    table.ForeignKey(
                        name: "FK_ArticleUsers_article_ArticlesId",
                        column: x => x.ArticlesId,
                        principalTable: "article",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ArticleUsers_users_UsersId",
                        column: x => x.UsersId,
                        principalTable: "users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Avatar",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    CreatedTime = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
                    IsActived = table.Column<bool>(type: "boolean", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false),
                    Remarks = table.Column<string>(type: "text", nullable: true),
                    UpdatedTime = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
                    UserId = table.Column<int>(type: "integer", nullable: false),
                    imgsId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Avatar", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "CommentUsers",
                columns: table => new
                {
                    UsersId = table.Column<int>(type: "integer", nullable: false),
                    commentsId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CommentUsers", x => new { x.UsersId, x.commentsId });
                    table.ForeignKey(
                        name: "FK_CommentUsers_Comments_commentsId",
                        column: x => x.commentsId,
                        principalTable: "Comments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_CommentUsers_users_UsersId",
                        column: x => x.UsersId,
                        principalTable: "users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "AvatarUsers",
                columns: table => new
                {
                    AvatarsId = table.Column<int>(type: "integer", nullable: false),
                    UsersId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AvatarUsers", x => new { x.AvatarsId, x.UsersId });
                    table.ForeignKey(
                        name: "FK_AvatarUsers_Avatar_AvatarsId",
                        column: x => x.AvatarsId,
                        principalTable: "Avatar",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AvatarUsers_users_UsersId",
                        column: x => x.UsersId,
                        principalTable: "users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_users_LikeId",
                table: "users",
                column: "LikeId");

            migrationBuilder.CreateIndex(
                name: "IX_imgs_ArticleId",
                table: "imgs",
                column: "ArticleId");

            migrationBuilder.CreateIndex(
                name: "IX_imgs_AvatarId",
                table: "imgs",
                column: "AvatarId");

            migrationBuilder.CreateIndex(
                name: "IX_imgs_CarouselId",
                table: "imgs",
                column: "CarouselId");

            migrationBuilder.CreateIndex(
                name: "IX_imgs_UsersId",
                table: "imgs",
                column: "UsersId");

            migrationBuilder.CreateIndex(
                name: "IX_article_RecommendId",
                table: "article",
                column: "RecommendId");

            migrationBuilder.CreateIndex(
                name: "IX_admin_RecommendId",
                table: "admin",
                column: "RecommendId");

            migrationBuilder.CreateIndex(
                name: "IX_ArticleUsers_UsersId",
                table: "ArticleUsers",
                column: "UsersId");

            migrationBuilder.CreateIndex(
                name: "IX_AvatarUsers_UsersId",
                table: "AvatarUsers",
                column: "UsersId");

            migrationBuilder.CreateIndex(
                name: "IX_CommentUsers_commentsId",
                table: "CommentUsers",
                column: "commentsId");

            migrationBuilder.AddForeignKey(
                name: "FK_admin_Recommends_RecommendId",
                table: "admin",
                column: "RecommendId",
                principalTable: "Recommends",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_article_Recommends_RecommendId",
                table: "article",
                column: "RecommendId",
                principalTable: "Recommends",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_imgs_article_ArticleId",
                table: "imgs",
                column: "ArticleId",
                principalTable: "article",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_imgs_Avatar_AvatarId",
                table: "imgs",
                column: "AvatarId",
                principalTable: "Avatar",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_imgs_carousel_CarouselId",
                table: "imgs",
                column: "CarouselId",
                principalTable: "carousel",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_imgs_users_UsersId",
                table: "imgs",
                column: "UsersId",
                principalTable: "users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_users_Like_LikeId",
                table: "users",
                column: "LikeId",
                principalTable: "Like",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
